//
//  ABUDCustomRewardedVideoAdapter.m
//  ABUDemo
//
//  Created by bytedance on 2021/10/20.
//  Copyright © 2021 bytedance. All rights reserved.
//

#import "JiGuangAdsRewardedVideoAdapter.h"

#import <JgAdsSDK/JgAdsRewardVodAd.h>

@interface JiGuangAdsRewardedVideoAdapter ()<JgAdsRewardVodAdDelegate>

@property (nonatomic, strong) JgAdsRewardVodAd *rewardVodAd;

@property (nonatomic, weak) UIViewController *viewController;

@property (nonatomic, assign) BOOL isBidType;
@end

@implementation JiGuangAdsRewardedVideoAdapter

- (BUMMediatedAdStatus)mediatedAdStatus {
    return BUMMediatedAdStatusNormal;
}

- (void)loadRewardedVideoAdWithSlotID:(nonnull NSString *)slotID andParameter:(nonnull NSDictionary *)parameter {
    _rewardVodAd = [JgAdsRewardVodAd new];
    _rewardVodAd.delegate = self;
    _rewardVodAd.controller = self.bridge.viewControllerForPresentingModalView;
    _rewardVodAd.posId = slotID;
    [_rewardVodAd loadAdData];
    if([parameter[BUMAdLoadingParamBiddingType] integerValue] == 1){
        self.isBidType = YES;
    }else{
        self.isBidType = NO;
    }
       if (self.isBidType) {
           NSLog(@"极光ads==========激励视频竞价广告");
       }

}

- (BOOL)showAdFromRootViewController:(UIViewController * _Nonnull)viewController parameter:(nonnull NSDictionary *)parameter {
    self.viewController = viewController;
    if (self.isBidType) {
        [self.rewardVodAd sendWinNotificationWithPrice:self.rewardVodAd.bidPrice];
    }
    [_rewardVodAd showFromRootViewController:self.viewController];
    return YES;
}
-(void)didReceiveBidResult:(BUMMediaBidResult *)result{
    
    if (!self.rewardVodAd) return;
    if (result.win) {
        NSLog(@"激励视频==========竞价成功!竞赢价格：%ld",(long)result.winnerPrice);
//        [self.rewardVodAd sendWinNotificationWithPrice:result.winnerPrice];
//        [_rewardVodAd showFromRootViewController:self.viewController];
    } else {
        NSLog(@"激励视频==========竞价失败!竞赢价格：%ld",(long)result.winnerPrice);
        JgAdsBiddingLossReason reason ;
        if ([result.lossDescription isEqualToString:@"timeout"]) {
            reason = JgAdsBiddingLossReasonOther;
        }else if ([result.lossDescription isEqualToString:@"low price"]){
            reason = JgAdsBiddingLossReasonLowPrice;
        }else {
            reason = JgAdsBiddingLossReasonOther;
        }
        [self.rewardVodAd sendWinFailNotificationReason:reason winnerPirce:result.winnerPrice];
       
    }
}

#pragma mark - JgAdsRewardVodAdDelegate

/**
 *  激励视频广告数据请求成功
 */
- (void)jgadsRewardVodAdSuccessToLoadAd:(JgAdsRewardVodAd *)rewardVodAd {
    NSMutableDictionary *parames = [NSMutableDictionary dictionary];
        if (self.isBidType && rewardVodAd.bidPrice > 0) {
#if DEBUG
            NSLog(@"极光ads激励视频价格=============%ld",(long)rewardVodAd.bidPrice);
#endif
            [parames setValue:[NSString stringWithFormat:@"%ld",rewardVodAd.bidPrice] forKey:BUMMediaAdLoadingExtECPM];
        }
    [self.bridge rewardedVideoAd:self didLoadWithExt:parames];
}

/**
 *  激励视频广告数据请求失败
 */
- (void)jgadsRewardVodAdFailToLoadAd:(JgAdsRewardVodAd *)rewardVodAd error:(NSError *)error {
    NSLog(@"极光ads激励视频加载失败：%@",error);
    [self.bridge rewardedVideoAd:self didLoadFailWithError:error ext:@{}];
}

/**
 *  激励视频广告视频缓存成功
 */
- (void)jgadsRewardVodAdVideoCacheFinish:(JgAdsRewardVodAd *)rewardVodAd {
    
}

/**
 *  激励视频广告渲染成功
 *  建议在此回调后展示广告
 */
- (void)jgadsRewardVodAdVideoReadyToPlay:(JgAdsRewardVodAd *)rewardVodAd {
    [self.bridge rewardedVideoAdVideoDidLoad:self];
}

/**
 *  激励视频广告播放失败
 *
 */
- (void)jgadsRewardVodAdVideoPlayFail:(JgAdsRewardVodAd *)rewardVodAd error:(NSError *)error {
    NSLog(@"极光ads激励视频播放失败：%@",error);
    [self.bridge rewardedVideoAd:self renderFailedWithError:error];
}

/**
 *  激励视频视图展示成功回调
 *  激励视频展示成功回调该函数
 */
- (void)jgadsRewardVodAdDidPresentScreen:(JgAdsRewardVodAd *)rewardVodAd {
    
}

/**
 *  激励视频广告视图展示失败回调
 *  激励视频广告展示失败回调该函数
 */
- (void)jgadsRewardVodAdFailToPresent:(JgAdsRewardVodAd *)rewardVodAd error:(NSError *)error {
    [self.bridge rewardedVideoAdDidShowFailed:self error:error];
}

/**
 *  激励视频广告曝光回调
 */
- (void)jgadsRewardVodAdWillExposure:(JgAdsRewardVodAd *)rewardVodAd {
    [self.bridge rewardedVideoAdDidVisible:self];
}

/**
 *  激励视频广告点击回调
 */
- (void)jgadsRewardVodAdClicked:(JgAdsRewardVodAd *)rewardVodAd {
    [self.bridge rewardedVideoAdDidClick:self];
}

/**
 *  激励视频广告页关闭
 */
- (void)jgadsRewardVodAdAdDidDismissClose:(JgAdsRewardVodAd *)rewardVodAd {
    [self.bridge rewardedVideoAdDidClose:self];
}

/**
 *  激励视频广告达到激励条件
 */
- (void)jgadsRewardVodAdAdDidEffective:(JgAdsRewardVodAd *)rewardVodAd {
    [self.bridge rewardedVideoAd:self didServerRewardSuccessWithInfo:^(BUMAdapterRewardAdInfo * _Nonnull info) {
            
    }];
}

/**
 *  激励视频广告视频播放结束
 */
- (void)jgadsRewardVodAdAdVideoPlayFinish:(JgAdsRewardVodAd *)rewardVodAd {
    [self.bridge rewardedVideoAd:self didPlayFinishWithError:nil];
}

/**
 *  激励视频广告关闭落地页
 */
- (void)jgadsRewardVodAdCloseLandingPage:(JgAdsRewardVodAd *)rewardVodAd{
    [self.bridge rewardedVideoAdWillDismissFullScreenModel:self];
}
@end
